package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Feedback;
import com.example.service.FeedbackService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("feedbacks")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;


    /**
     * 反馈信息分页查询
     * @return
     */
    @GetMapping("/getPageFeedback/{current}/{size}/{code}")
    public R getPageFeedback(@PathVariable int current,@PathVariable int size,@PathVariable int code,Feedback feedback){


        IPage page =new Page(current,size);
        LambdaQueryWrapper<Feedback> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(feedback.getJobName()),Feedback::getJobName,feedback.getJobName());

        if (code==1){
            lqw.eq(Strings.isNotEmpty(feedback.getCommercialId()),Feedback::getCommercialId,feedback.getCommercialId());
        }else if (code==2){
            lqw.eq(Strings.isNotEmpty(feedback.getStuNumber()),Feedback::getStuNumber,feedback.getStuNumber());
        }

        IPage page1 = feedbackService.page(page,lqw);

        return R.success("反馈信息分页查询！",page1);
    }

    /**
     * 查询所有反馈信息
     * @return
     */
    @GetMapping("/getAllFeedback")
    public R getAllFeedback(){

        List<Feedback> list = feedbackService.list();
        return R.success("查询所有反馈信息！",list);
    }

    /**
     * 添加反馈信息
     * @return
     */
    @PostMapping("/addFeedback")
    public R addFeedback(@RequestBody Feedback feedback){

        boolean save = feedbackService.save(feedback);
        if (save){
            return R.success("添加反馈信息成功！",null);
        }else {
            return R.error("添加反馈信息失败！");
        }
    }

    /**
     * 通过id修改反馈信息
     * @return
     */
    @PutMapping("/updateFeedback")
    public R updateFeedback(@RequestBody Feedback feedback){
        boolean b = feedbackService.updateById(feedback);
        if (b){
           return R.success("通过id修改反馈信息成功",feedback);
        }else {
           return R.error("通过id修改反馈信息失败！");
        }
    }

    /**
     * 通过id删除反馈信息
     * @return
     */
    @DeleteMapping("/deleteFeedback/{id}")
    public R deleteFeedback(@PathVariable int id){

        boolean b = feedbackService.removeById(id);
        if (b){
           return R.success("通过id删除反馈信息成功",id);
        }else {
           return R.error("通过id删除反馈信息失败！");
        }
    }

}

